package tutorial.lite_mod.exam_system;


import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.transaction.Transaction;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.apache.log4j.Logger;
import public_util.model.*;
import public_util.pack_4_stable.tools.DateTool;
import public_util.pack_4_stable.tools.EnCodeTool;
import public_util.pack_4_stable.tools.NumberTool;
import public_util.servlet.ServletParent;
import system_api_4_trade.dao.ApiPublicGoodsOrderDetailMapper;
import system_api_4_trade.dao.ApiPublicMiddleTableMapper;
import system_api_4_trade.dao.ApiPublicUserInfoMapper;
import system_api_4_trade.util.sql.InsertTool;
import system_api_4_trade.util.sql.SelectTool;
import system_api_4_trade.util.sql.UpdateTool;
import system_api_4_trade.util.tool_4_handle.DateToolApi;
import system_api_4_trade.util.tool_4_handle.StringToolApi;
import system_api_4_trade.util.tool_4_handle.Tool_4_ServletApi;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 控制层用到的具体方法
 */
class Tutorial4LiteModTool {



    private static Logger logger = Logger.getLogger(Tutorial4LiteModTool.class);

    /**
     * 生成学生的考试卷, 传入传入课程的商品ID, 以及学生的ID
     */
    public static ReturnData generate_exam_4_student(SqlSessionFactory sqlSession, ServletParent thisParent, HttpSession hsession, HttpServletResponse response, HttpServletRequest request){

        logger.info("----generate_exam_4_student----");
        ReturnData resultData = new ReturnData();
        PublicDataAdminUser adminUser = (PublicDataAdminUser) Tool_4_ServletApi.getAttribute(null, hsession,Const4PublicDataBase.MEM_4_self_public_data_admin_user);
        PublicAgentInfo agentInfo = (PublicAgentInfo)Tool_4_ServletApi.getAttribute(null, hsession,Const4PublicDataBase.MEM_4_self_public_agent_info);
        resultData = Tool_4_ServletApi.checkAccessType(hsession, resultData, 102);
        if (resultData.getStatus() != 0)
        {
            return resultData;
        }

        String id_admin = "-1";

        if (agentInfo != null)
        {
            id_admin = agentInfo.getId_public_data_admin_user() + "";
        }

        if (adminUser != null)
        {
            id_admin = adminUser.getId_public_data_admin_user() + "";
        }

        String id_public_middle_table = request.getParameter(Const4PublicDataBase.PARA_id_public_middle_table);
        id_public_middle_table = StringToolApi.paraHttpFilter(id_public_middle_table);
        if (id_public_middle_table == null)
        {
            resultData.setMsg("课程ID未传入");
            return resultData;
        }

        String mid_table_remark_001 = request.getParameter(Const4PublicDataBase.PARA_mid_table_remark_001);
        String mid_table_remark_002 = request.getParameter(Const4PublicDataBase.PARA_mid_table_remark_002);
        String mid_table_remark_003 = request.getParameter(Const4PublicDataBase.PARA_mid_table_remark_003);
        if (mid_table_remark_001 == null)
        {
            resultData.setMsg("mid_table_remark_001-合格标准未传入");
            return resultData;
        }
        if (mid_table_remark_002 == null)
        {
            resultData.setMsg("mid_table_remark_002-开卷闭卷未传入");
            return resultData;
        }
        if (mid_table_remark_003 == null)
        {
            resultData.setMsg("mid_table_remark_003-考试时长未传入");
            return resultData;
        }


        String id_public_user_info = request.getParameter(Const4PublicDataBase.PARA_id_public_user_info);
        id_public_user_info = StringToolApi.paraHttpFilter(id_public_user_info);
        if (id_public_user_info == null)
        {
            resultData.setMsg("用户ID未传入");
            return resultData;
        }
        SqlSession session = sqlSession.openSession();
        TransactionFactory transactionFactory = new JdbcTransactionFactory();
        Transaction newTransaction = transactionFactory.newTransaction(session.getConnection(), false);
        ApiPublicMiddleTableMapper mMapper = session.getMapper(ApiPublicMiddleTableMapper.class);
//        PublicGoodsInfoMapper infoMapper = session.getMapper(PublicGoodsInfoMapper.class);
        ApiPublicUserInfoMapper uiMapper = session.getMapper(ApiPublicUserInfoMapper.class);
        ApiPublicGoodsOrderDetailMapper dtMapper = session.getMapper(ApiPublicGoodsOrderDetailMapper.class);
        try {

            //查询用户信息和课程信息
            PublicMiddleTable kechengData = find_public_middle_table_kecheng(mMapper, id_public_middle_table);


            if (kechengData == null)
            {
                resultData.setMsg("课程信息不存在");
                return resultData;
            }

            HashMap<String, Object> paraDataA = new HashMap<String, Object>();
            paraDataA.put(Const4PublicDataBase.PARA_id_public_user_info, id_public_user_info);
            PublicUserInfo uInfo = uiMapper.findUserById(paraDataA);

            if (uInfo == null)
            {
                resultData.setMsg("用户信息不存在");
                return resultData;
            }


            String num_danxuan = kechengData.getMid_table_remark_004();
            String num_duoxuan = kechengData.getMid_table_remark_005();
            String num_panduan = kechengData.getMid_table_remark_005();

            int ti_danxuan = 0;
            int ti_duoxuan = 0;
            int ti_panduan = 0;

            try
            {
                ti_danxuan = Integer.valueOf(num_danxuan);
            }catch(Exception e)
            {

            }

            try
            {
                ti_duoxuan = Integer.valueOf(num_duoxuan);
            }catch(Exception e)
            {

            }

            try
            {
                ti_panduan = Integer.valueOf(num_panduan);
            }catch(Exception e)
            {

            }


            //单选
            List<PublicMiddleTable> shitiList_danxuan = find_public_middle_table_timu(mMapper, id_public_middle_table, "单选");
            //多选
            List<PublicMiddleTable> shitiList_duoxuan = find_public_middle_table_timu(mMapper, id_public_middle_table, "多选");
            //判断
            List<PublicMiddleTable> shitiList_panduan = find_public_middle_table_timu(mMapper, id_public_middle_table, "判断");



            //搞完之后，插入数据
            String exam_name = kechengData.getMid_table_remark_001() + "-" + DateToolApi.getCurrentDateStr();
            String exam_id = EnCodeTool.enCodeTime(new Date()) + id_public_user_info + id_public_middle_table;

            HashMap<String, Object> param = new HashMap<String, Object>();
            param.put(Const4PublicDataBase.PARA_type_middle_app, Const4EnumInDB.type_middle_app_class_info);
            param.put(Const4PublicDataBase.PARA_type_inner_app, "10014");//试卷名称
            param.put(Const4PublicDataBase.PARA_is_online, "0");//没有上线
            param.put(Const4PublicDataBase.PARA_time_4_begin,  new Date());//没有上线

            param.put(Const4PublicDataBase.PARA_mid_table_remark_001, mid_table_remark_001);
            param.put(Const4PublicDataBase.PARA_mid_table_remark_002, mid_table_remark_002);
            param.put(Const4PublicDataBase.PARA_mid_table_remark_003, mid_table_remark_003); //考试时间长短
            param.put(Const4PublicDataBase.PARA_mid_table_remark_004, "未知"); //考试结果
            param.put(Const4PublicDataBase.PARA_mid_table_remark_025, uInfo.getNick_name()); //考试人员姓名
            param.put(Const4PublicDataBase.PARA_mid_table_remark_014, exam_name);
            param.put(Const4PublicDataBase.PARA_mid_table_remark_015, exam_id);
            param.put(Const4PublicDataBase.PARA_mid_table_remark_018, kechengData.getMid_table_remark_001());
            param.put(Const4PublicDataBase.PARA_mid_table_remark_019, id_public_middle_table);
            param.put(Const4PublicDataBase.PARA_id_public_user_info, id_public_user_info);
            param.put(Const4PublicDataBase.PARA_id_public_data_admin_user, id_admin);
            param.put(Const4PublicDataBase.PARA_time_insert, new Date());
            HashMap<String, Object> selMap = InsertTool.paramap2Insert(param, "public_middle_table_lite_mod_exam_system");
            mMapper.insertPublicMiddleTable(selMap);
            //


            genQuestion(shitiList_danxuan, ti_danxuan, exam_name, exam_id, uInfo, mMapper);
            genQuestion(shitiList_duoxuan, ti_duoxuan, exam_name, exam_id, uInfo, mMapper);
            genQuestion(shitiList_panduan, ti_panduan, exam_name, exam_id, uInfo, mMapper);
            //单选、多选、判断题
            session.commit();
            //查询试卷
            HashMap<String, Object> paraMap = new HashMap<String, Object>();
            paraMap.put(Const4PublicDataBase.PARA_type_middle_app, Const4EnumInDB.type_middle_app_class_info);
            paraMap.put(Const4PublicDataBase.PARA_type_inner_app, 10014);//搜索题库
            paraMap.put(Const4PublicDataBase.PARA_mid_table_remark_015+ "_in", exam_id);
            HashMap<String, Object>  realPara = SelectTool.paramap2selectMap(paraMap, "public_middle_table_lite_mod_exam_system");
            List<PublicMiddleTable> shijuan = mMapper.tableDataList(realPara);//查询是否具备值
            //查询考题
            paraMap = new HashMap<String, Object>();
            paraMap.put(Const4PublicDataBase.PARA_type_middle_app, Const4EnumInDB.type_middle_app_class_info);
            paraMap.put(Const4PublicDataBase.PARA_type_inner_app, 10115);//搜索题库
            paraMap.put(Const4PublicDataBase.PARA_mid_table_remark_015+ "_in", exam_id);
            realPara = SelectTool.paramap2selectMap(paraMap, "public_middle_table_lite_mod_exam_system");
            List<PublicMiddleTable> kaoti = mMapper.tableDataList(realPara);//查询是否具备值

            HashMap<String, Object> returnData = new HashMap<String, Object>();
            returnData.put("shijuan", shijuan);
            returnData.put("shijuan_kaoti", kaoti);
            resultData.setStatus(0);
            resultData.setReturnData(returnData);
            return resultData;
        }
        catch(Exception e)
        {
            try {
                newTransaction.rollback();
                String msg = Tool_4_ServletApi.showStackTrace(e);
                resultData.setMsg(msg);
                return resultData;

            } catch (SQLException e1) {
                String msg = Tool_4_ServletApi.showStackTrace(e);
                resultData.setMsg(msg);
                return resultData;
            }
        }
        finally
        {
            session.close();
        }
    }


    private static List<PublicMiddleTable> genQuestion(List<PublicMiddleTable> source, int q_len, String exam_name, String exam_id, PublicUserInfo userInfo, ApiPublicMiddleTableMapper mMapper)
    {
        if (q_len == 0)
        {
            return null;
        }

        if (source == null || source.size() == 0)
        {
            return null;
        }

        int souce_len = source.size();

        ArrayList<Integer> len_int_list = NumberTool.findRandomIdx(souce_len, q_len);


        for ( int i = 0; i < len_int_list.size(); i++)
        {
            PublicMiddleTable shiti_item = source.get(len_int_list.get(i));
            HashMap<String, Object> param = new HashMap<String, Object>();
            param.put(Const4PublicDataBase.PARA_type_middle_app, Const4EnumInDB.type_middle_app_class_info);
            param.put(Const4PublicDataBase.PARA_type_inner_app, "10115");//试题
            param.put(Const4PublicDataBase.PARA_is_online, "0");//没有上线

            param.put(Const4PublicDataBase.PARA_mid_table_remark_004, shiti_item.getMid_table_remark_004()); //
            param.put(Const4PublicDataBase.PARA_mid_table_remark_005, shiti_item.getMid_table_remark_005()); //
            param.put(Const4PublicDataBase.PARA_mid_table_remark_006, shiti_item.getMid_table_remark_006()); //
            param.put(Const4PublicDataBase.PARA_mid_table_remark_007, shiti_item.getMid_table_remark_007()); //

            param.put(Const4PublicDataBase.PARA_mid_table_remark_014, exam_name); //
            param.put(Const4PublicDataBase.PARA_mid_table_remark_015, exam_id);
            param.put(Const4PublicDataBase.PARA_mid_table_remark_016, shiti_item.getMid_table_remark_016()); //
            param.put(Const4PublicDataBase.PARA_mid_table_remark_017, shiti_item.getMid_table_remark_017()); //
            param.put(Const4PublicDataBase.PARA_mid_table_remark_018, shiti_item.getMid_table_remark_018()); //
            param.put(Const4PublicDataBase.PARA_mid_table_remark_019, shiti_item.getMid_table_remark_019()); //
            param.put(Const4PublicDataBase.PARA_mid_table_remark_025, userInfo.getNick_name()); //
            param.put(Const4PublicDataBase.PARA_id_public_user_info, userInfo.getId_public_user_info());
            param.put(Const4PublicDataBase.PARA_tag_inner_mid_table, shiti_item.getTag_inner_mid_table());
            param.put(Const4PublicDataBase.PARA_mid_table_remark_021, shiti_item.getId_public_middle_table());//试题ID
            param.put(Const4PublicDataBase.PARA_time_insert, new Date());

            HashMap<String, Object> selMap = InsertTool.paramap2Insert(param, "public_middle_table_lite_mod_exam_system");
            mMapper.insertPublicMiddleTable(selMap);
        }
        return null;
    }





    //查看课程
    public static PublicMiddleTable find_public_middle_table_kecheng(ApiPublicMiddleTableMapper midMapper, String id_mid)
    {
        HashMap<String, Object> paraPrice = new HashMap<String, Object>();
        paraPrice.put(Const4PublicDataBase.PARA_type_middle_app, 1208);
        paraPrice.put(Const4PublicDataBase.PARA_type_inner_app, 10003);
        paraPrice.put(Const4PublicDataBase.PARA_is_online, 0);
        paraPrice.put(Const4PublicDataBase.PARA_id_public_middle_table+ "_in", id_mid);
        HashMap<String, Object>  realPara = SelectTool.paramap2selectMap(paraPrice, "public_middle_table_lite_mod_exam_system");
        List<PublicMiddleTable> listQianyuePrice = midMapper.tableDataList(realPara);//查询是否具备值
        if (listQianyuePrice == null || listQianyuePrice.size() == 0)
        {
            return null;
        }
        return listQianyuePrice.get(0);
    }



    //查看题目
    public static List<PublicMiddleTable> find_public_middle_table_timu(ApiPublicMiddleTableMapper midMapper, String kechengId, String r004)
    {
        //单选
        HashMap<String, Object> paraMapDanxuan = new HashMap<String, Object>();
        paraMapDanxuan.put(Const4PublicDataBase.PARA_type_middle_app, 1208);
        paraMapDanxuan.put(Const4PublicDataBase.PARA_type_inner_app, 10005);//搜索题库
        paraMapDanxuan.put(Const4PublicDataBase.PARA_mid_table_remark_019, kechengId);//课程ID
        paraMapDanxuan.put(Const4PublicDataBase.PARA_mid_table_remark_004, r004);//
        paraMapDanxuan.put(Const4PublicDataBase.PARA_is_online, 0);//
        HashMap<String, Object>  realPara = SelectTool.paramap2selectMap(paraMapDanxuan, "public_middle_table_lite_mod_exam_system");
        List<PublicMiddleTable> shiti_list = midMapper.tableDataList(realPara);//查询是否具备值
        return shiti_list;
    }




    /**
     * 查看自测题
     */
    public static ReturnData show_inner_exam_4_student(SqlSessionFactory sqlSession, ServletParent thisParent, HttpSession hsession,HttpServletResponse response, HttpServletRequest request){

        logger.info("----show_inner_exam_4_student----");

        ReturnData resultData = new ReturnData();

        PublicDataAdminUser adminUser = (PublicDataAdminUser)Tool_4_ServletApi.getAttribute(null, hsession,Const4PublicDataBase.MEM_4_self_public_data_admin_user);
        PublicAgentInfo agentInfo = (PublicAgentInfo)Tool_4_ServletApi.getAttribute(null, hsession,Const4PublicDataBase.MEM_4_self_public_agent_info);
        resultData = Tool_4_ServletApi.checkAccessType(hsession, resultData, 102);
        if (resultData.getStatus() != 0)
        {
            return resultData;
        }

        String id_admin = "-1";

        if (agentInfo != null)
        {
            id_admin = agentInfo.getId_public_data_admin_user() + "";
        }

        if (adminUser != null)
        {
            id_admin = adminUser.getId_public_data_admin_user() + "";
        }

        String id_public_middle_table = request.getParameter(Const4PublicDataBase.PARA_id_public_middle_table);
        id_public_middle_table = StringToolApi.paraHttpFilter(id_public_middle_table);
        if (id_public_middle_table == null)
        {
            resultData.setMsg("课程ID未传入");
            return resultData;
        }
        String id_public_user_info = request.getParameter(Const4PublicDataBase.PARA_id_public_user_info);
        id_public_user_info = StringToolApi.paraHttpFilter(id_public_user_info);
        if (id_public_user_info == null)
        {
            resultData.setMsg("用户ID未传入");
            return resultData;
        }
        SqlSession session = sqlSession.openSession();
        TransactionFactory transactionFactory = new JdbcTransactionFactory();
        Transaction newTransaction = transactionFactory.newTransaction(session.getConnection(), false);
        ApiPublicMiddleTableMapper mMapper = session.getMapper(ApiPublicMiddleTableMapper.class);
        ApiPublicUserInfoMapper uiMapper = session.getMapper(ApiPublicUserInfoMapper.class);
        try {

            //查询用户信息和课程信息
            //查询用户信息和课程信息
            PublicMiddleTable kechengData = find_public_middle_table_kecheng(mMapper, id_public_middle_table);


            if (kechengData == null)
            {
                resultData.setMsg("课程信息不存在");
                return resultData;
            }

            HashMap<String, Object> paraDataA = new HashMap<String, Object>();
            paraDataA.put(Const4PublicDataBase.PARA_id_public_user_info, id_public_user_info);
            PublicUserInfo uInfo = uiMapper.findUserById(paraDataA);

            if (uInfo == null)
            {
                resultData.setMsg("用户信息不存在");
                return resultData;
            }


            String r019 = id_public_middle_table; //课程ID
            //查询题库全部试题
            HashMap<String, Object> paraMap = new HashMap<String, Object>();
            paraMap.put(Const4PublicDataBase.PARA_type_middle_app, Const4EnumInDB.type_middle_app_class_info);
            paraMap.put(Const4PublicDataBase.PARA_type_inner_app, 10005);//搜索题库
            paraMap.put(Const4PublicDataBase.PARA_mid_table_remark_019, r019);//课程ID
            paraMap.put(Const4PublicDataBase.PARA_is_online, 0);//
            HashMap<String, Object>  realPara = SelectTool.paramap2selectMap(paraMap, "public_middle_table_lite_mod_exam_system");
            List<PublicMiddleTable> tikuList = mMapper.tableDataList(realPara);//查询是否具备值
            if (tikuList == null || tikuList.size() == 0)
            {
                resultData.setMsg("题库没有试题");
                return resultData;

            }


            String exam_name = kechengData.getId_public_middle_table() + "-"  + "默认考试题-" + uInfo.getNick_name();
            String exam_id = "this_is_default_exam-" + id_public_user_info + id_public_middle_table;

            //插入默认试卷
            List<PublicMiddleTable> defualt_shijun =  insertDefault(session, exam_id, id_public_middle_table, mMapper, id_public_user_info, uInfo.getNick_name(), exam_name, kechengData.getMid_table_remark_001(), id_public_middle_table+"", id_admin);


            //查询默认考试题
            paraMap = new HashMap<String, Object>();
            paraMap.put(Const4PublicDataBase.PARA_type_middle_app, Const4EnumInDB.type_middle_app_class_info);
            paraMap.put(Const4PublicDataBase.PARA_type_inner_app, 10115);//搜索题库
            paraMap.put(Const4PublicDataBase.PARA_mid_table_remark_015 + "_in", exam_id);
            realPara = SelectTool.paramap2selectMap(paraMap, "public_middle_table_lite_mod_exam_system");
            List<PublicMiddleTable> morenkaoti = mMapper.tableDataList(realPara);//查询是否具备值


            for (int i = 0; i < tikuList.size(); i++)
            {
                PublicMiddleTable tikuItem = tikuList.get(i);
                tikuItem.setMid_table_remark_021(tikuItem.getId_public_middle_table() + "");//源头考题拿过来, 这里一般都没有设置
                for (int k = 0; k < morenkaoti.size(); k++)
                {
                    PublicMiddleTable morenItem = morenkaoti.get(k);
                    String r_021 = morenItem.getMid_table_remark_021();
                    String r_020 = morenItem.getMid_table_remark_020();
                    String r_022 = morenItem.getMid_table_remark_022();
                    String id_middle_table = tikuItem.getId_public_middle_table() + "";
                    if (id_middle_table.equals(r_021))
                    {
                        tikuItem.setMid_table_remark_020(r_020);
                        tikuItem.setMid_table_remark_022(r_022);
                    }
                }
            }
            HashMap<String, Object> returnData = new HashMap<String, Object>();
            returnData.put("shijuan", defualt_shijun);
            returnData.put("shijuan_kaoti", tikuList); //题库的考试题

            resultData.setStatus(0);
            resultData.setReturnData(returnData);
            return resultData;
        }
        catch(Exception e)
        {
            try {
                newTransaction.rollback();
                String msg = Tool_4_ServletApi.showStackTrace(e);
                resultData.setMsg(msg);
                return resultData;

            } catch (SQLException e1) {
                String msg = Tool_4_ServletApi.showStackTrace(e);
                resultData.setMsg(msg);
                return resultData;
            }
        }
        finally
        {
            session.close();
        }
    }




    private static List<PublicMiddleTable> insertDefault(SqlSession session, String exam_id, String id_public_goods_info, ApiPublicMiddleTableMapper mMapper, String id_public_user_info, String mid_table_remark_025, String mid_table_remark_014, String mid_table_remark_018, String mid_table_remark_019, String id_admin)
    {
        //查询默认试题
        HashMap<String, Object> paraMap = new HashMap<String, Object>();
        paraMap.put(Const4PublicDataBase.PARA_type_middle_app, Const4EnumInDB.type_middle_app_class_info);
        paraMap.put(Const4PublicDataBase.PARA_type_inner_app, 10014);//查询默认试卷
        paraMap.put(Const4PublicDataBase.PARA_is_online, 0);//
        paraMap.put(Const4PublicDataBase.PARA_mid_table_remark_015+ "_in", exam_id);

        HashMap<String, Object>  realPara = SelectTool.paramap2selectMap(paraMap, "public_middle_table_lite_mod_exam_system");
        List<PublicMiddleTable> shijuanList = mMapper.tableDataList(realPara);//查询是否具备值

        if (shijuanList == null || shijuanList.size() == 0)
        {
            HashMap<String, Object> param = new HashMap<String, Object>();
            param.put(Const4PublicDataBase.PARA_type_middle_app, Const4EnumInDB.type_middle_app_class_info);
            param.put(Const4PublicDataBase.PARA_type_inner_app, "10014");//试卷名称
            param.put(Const4PublicDataBase.PARA_is_online, "0");//没有上线

            param.put(Const4PublicDataBase.PARA_mid_table_remark_001, "0.6");
            param.put(Const4PublicDataBase.PARA_mid_table_remark_002, "闭卷");
            param.put(Const4PublicDataBase.PARA_mid_table_remark_003, null); //考试时间长短
            param.put(Const4PublicDataBase.PARA_mid_table_remark_004, "未知"); //考试结果
            param.put(Const4PublicDataBase.PARA_mid_table_remark_025, mid_table_remark_025); //考试人员姓名
            param.put(Const4PublicDataBase.PARA_mid_table_remark_014, mid_table_remark_014);
            param.put(Const4PublicDataBase.PARA_mid_table_remark_015, exam_id);
            param.put(Const4PublicDataBase.PARA_mid_table_remark_018, mid_table_remark_018);
            param.put(Const4PublicDataBase.PARA_mid_table_remark_019, mid_table_remark_019);
            param.put(Const4PublicDataBase.PARA_id_public_user_info, id_public_user_info);
            param.put(Const4PublicDataBase.PARA_id_public_data_admin_user, id_admin);
            param.put(Const4PublicDataBase.PARA_time_insert, new Date());
            HashMap<String, Object> selMap = InsertTool.paramap2Insert(param, "public_middle_table_lite_mod_exam_system");
            mMapper.insertPublicMiddleTable(selMap);
            session.commit();
        }

        shijuanList = mMapper.tableDataList(realPara);//查询是否具备值
        return shijuanList;
    }





    /**
     * 临时的试题, 需要保存在习题集中type_save=101错题; 102表示收藏
     *
     * 习题，或者真实的考试中, 都会调用这个ID
     */
    public static ReturnData favorate_or_wrong_question(SqlSessionFactory sqlSession, ServletParent thisParent, HttpSession hsession,HttpServletResponse response, HttpServletRequest request){

        logger.info("----favorate_or_wrong_question----");

        ReturnData resultData = new ReturnData();

        PublicAgentInfo agentInfo = (PublicAgentInfo)Tool_4_ServletApi.getAttribute(null, hsession,Const4PublicDataBase.MEM_4_self_public_agent_info);
        PublicUserInfo userInfo = (PublicUserInfo)Tool_4_ServletApi.getAttribute(null, hsession,Const4PublicDataBase.MEM_4_self_public_user_info);

        resultData = Tool_4_ServletApi.checkAccessType(hsession, resultData, 101);
        if (resultData.getStatus() != 0)
        {
            return resultData;
        }

        String id_admin = "-1";

        if (agentInfo != null)
        {
            id_admin = agentInfo.getId_public_data_admin_user() + "";
        }


        //
        String mid_table_remark_021 = request.getParameter(Const4PublicDataBase.PARA_mid_table_remark_021);
        mid_table_remark_021 = StringToolApi.paraHttpFilter(mid_table_remark_021);
        if (mid_table_remark_021 == null)
        {
            resultData.setMsg("错题ID没有传入");
            return resultData;
        }


        String type_save = request.getParameter("type_save"); //收藏的方式
        type_save = StringToolApi.paraHttpFilter(type_save);
        if (type_save == null)
        {
            resultData.setMsg("收藏的方式未填入");
            return resultData;
        }
        SqlSession session = sqlSession.openSession();
        TransactionFactory transactionFactory = new JdbcTransactionFactory();
        Transaction newTransaction = transactionFactory.newTransaction(session.getConnection(), false);
        ApiPublicMiddleTableMapper mMapper = session.getMapper(ApiPublicMiddleTableMapper.class);
        try {

            //查询试题

            HashMap<String, Object> paraMap = new HashMap<String, Object>();
            paraMap.put(Const4PublicDataBase.PARA_type_middle_app, Const4EnumInDB.type_middle_app_class_info);
            paraMap.put(Const4PublicDataBase.PARA_type_inner_app, 10005);//库中试题
            paraMap.put(Const4PublicDataBase.PARA_id_public_middle_table, mid_table_remark_021);//

            HashMap<String, Object> realPara = SelectTool.paramap2selectMap(paraMap, "public_middle_table_lite_mod_exam_system");
            List<PublicMiddleTable> currentList = mMapper.tableDataList(realPara);//查询是否具备值


            if (currentList == null || currentList.size() == 0)
            {
                resultData.setMsg("库中的试题不存在");
                return resultData;
            }

            PublicMiddleTable current_kaoti = currentList.get(0);

            int id_public_user_info = userInfo.getId_public_user_info();
            String mid_table_remark_019 = current_kaoti.getMid_table_remark_019(); //课程ID

            String exam_id = "this_is_default_exam-" + id_public_user_info + mid_table_remark_019;


            String id_public_goods_info = current_kaoti.getId_public_user_info() + "";
            String mid_table_remark_025 = current_kaoti.getMid_table_remark_025();
            String mid_table_remark_018 = current_kaoti.getMid_table_remark_018();
            String mid_table_remark_014 = current_kaoti.getMid_table_remark_014();
            List<PublicMiddleTable> defulat_shujuan = insertDefault(session, exam_id, id_public_goods_info, mMapper, id_public_user_info + "",mid_table_remark_025, mid_table_remark_014, mid_table_remark_018, mid_table_remark_019, id_admin);
            if (defulat_shujuan == null || defulat_shujuan.size() == 0)
            {
                resultData.setMsg("默认试卷不存在");
                return resultData;
            }
            //默认试卷中,是否有考题
            paraMap = new HashMap<String, Object>();
            paraMap.put(Const4PublicDataBase.PARA_type_middle_app, Const4EnumInDB.type_middle_app_class_info);
            paraMap.put(Const4PublicDataBase.PARA_type_inner_app, 10115);//考试试题
            paraMap.put(Const4PublicDataBase.PARA_mid_table_remark_015+ "_in", exam_id);
            paraMap.put(Const4PublicDataBase.PARA_mid_table_remark_021, mid_table_remark_021);//是否存在考题


            realPara = SelectTool.paramap2selectMap(paraMap, "public_middle_table_lite_mod_exam_system");
            List<PublicMiddleTable> currentDefualtList = mMapper.tableDataList(realPara);//查询是否具备值


            if (currentDefualtList == null || currentDefualtList.size() == 0)
            {
                //不存在试题,则插入
                HashMap<String, Object> param = new HashMap<String, Object>();
                param.put(Const4PublicDataBase.PARA_type_middle_app, Const4EnumInDB.type_middle_app_class_info);
                param.put(Const4PublicDataBase.PARA_type_inner_app, "10115");//试题
                param.put(Const4PublicDataBase.PARA_is_online, "0");//没有上线

                param.put(Const4PublicDataBase.PARA_mid_table_remark_004, current_kaoti.getMid_table_remark_004()); //
                param.put(Const4PublicDataBase.PARA_mid_table_remark_005, current_kaoti.getMid_table_remark_005()); //
                param.put(Const4PublicDataBase.PARA_mid_table_remark_006, current_kaoti.getMid_table_remark_006()); //
                param.put(Const4PublicDataBase.PARA_mid_table_remark_007, current_kaoti.getMid_table_remark_007()); //

                param.put(Const4PublicDataBase.PARA_mid_table_remark_014, mid_table_remark_014); //
                param.put(Const4PublicDataBase.PARA_mid_table_remark_015, exam_id);
                param.put(Const4PublicDataBase.PARA_mid_table_remark_016, current_kaoti.getMid_table_remark_016()); //
                param.put(Const4PublicDataBase.PARA_mid_table_remark_017, current_kaoti.getMid_table_remark_017()); //
                param.put(Const4PublicDataBase.PARA_mid_table_remark_018, current_kaoti.getMid_table_remark_018()); //
                param.put(Const4PublicDataBase.PARA_mid_table_remark_019, current_kaoti.getMid_table_remark_019()); //
                param.put(Const4PublicDataBase.PARA_mid_table_remark_025, mid_table_remark_025); //
                param.put(Const4PublicDataBase.PARA_id_public_user_info, id_public_user_info);
                param.put(Const4PublicDataBase.PARA_tag_inner_mid_table, current_kaoti.getTag_inner_mid_table());
                param.put(Const4PublicDataBase.PARA_mid_table_remark_021, current_kaoti.getId_public_middle_table());//试题ID
                param.put(Const4PublicDataBase.PARA_time_insert, new Date());
                HashMap<String, Object> selMap = InsertTool.paramap2Insert(param, "public_middle_table_lite_mod_exam_system");
                mMapper.insertPublicMiddleTable(selMap);


                session.commit();
                currentDefualtList = mMapper.tableDataList(realPara);//查询是否具备值
            }

            if (currentDefualtList == null || currentDefualtList.size() == 0)
            {
                resultData.setMsg("插入考试题失败");
                return resultData;
            }

            PublicMiddleTable kaotiInfDefault = currentDefualtList.get(0);
            paraMap = new HashMap<String, Object>();
            paraMap.put("pre_db_para_type_middle_app", Const4EnumInDB.type_middle_app_class_info);
            paraMap.put("pre_db_para_type_inner_app", 10115);
            paraMap.put("pre_db_para_mid_table_remark_015", exam_id);//默认试卷

            //type_save=101错题; 102表示收藏
            if ("101".equals(type_save))
            {
                paraMap.put(Const4PublicDataBase.PARA_mid_table_remark_022, 102);
            }
            if ("102".equals(type_save)) //收藏
            {
                paraMap.put(Const4PublicDataBase.PARA_mid_table_remark_020, 101);
            }
            if ("202".equals(type_save)) //取消收藏
            {
                paraMap.put(Const4PublicDataBase.PARA_mid_table_remark_020, 102);
            }
            if ("103".equals(type_save))
            {
                paraMap.put(Const4PublicDataBase.PARA_mid_table_remark_022, 101);
            }
            paraMap.put(Const4PublicDataBase.PARA_mid_table_remark_027, 101);//表示这个题已经做过
            paraMap.put("pre_db_para_id_public_middle_table", kaotiInfDefault.getId_public_middle_table());
            mMapper.updatePublicMiddleTable(UpdateTool.paramap2updateMap(paraMap, "public_middle_table_lite_mod_exam_system"));
            session.commit();
            resultData.setStatus(0);
            return resultData;
        }
        catch(Exception e)
        {
            try {
                newTransaction.rollback();
                String msg = Tool_4_ServletApi.showStackTrace(e);
                resultData.setMsg(msg);
                return resultData;

            } catch (SQLException e1) {
                String msg = Tool_4_ServletApi.showStackTrace(e1);
                resultData.setMsg(msg);
                return resultData;
            }
        }
        finally
        {
            session.close();
        }
    }




    /**
     * 帮助阿姨生成课程, 传入传入课程的商品ID, 以及学生的ID
     * id_public_goods_info 1086
     * id_public_user_info: 阿姨的ID
     * @param hsession
     * @param response
     * @return
     */
    public static ReturnData submit_exam(SqlSessionFactory sqlSession, ServletParent thisParent, HttpSession hsession,HttpServletResponse response, HttpServletRequest request){

        logger.info("----submit_exam----");

        ReturnData resultData = new ReturnData();

        PublicDataAdminUser adminUser = (PublicDataAdminUser)Tool_4_ServletApi.getAttribute(null, hsession,Const4PublicDataBase.MEM_4_self_public_data_admin_user);
        PublicAgentInfo agentInfo = (PublicAgentInfo)Tool_4_ServletApi.getAttribute(null, hsession,Const4PublicDataBase.MEM_4_self_public_agent_info);

//        resultData = Tool_4_ServletApi.checkAccessType(hsession, resultData, 102);
//        if (resultData.getStatus() != 0)
//        {
//            return resultData;
//        }

        String mid_table_remark_015 = request.getParameter(Const4PublicDataBase.PARA_mid_table_remark_015);
        mid_table_remark_015 = StringToolApi.paraHttpFilter(mid_table_remark_015);
        if (mid_table_remark_015 == null)
        {
            resultData.setMsg("试卷ID未传入");
            return resultData;
        }
        SqlSession session = sqlSession.openSession();
        TransactionFactory transactionFactory = new JdbcTransactionFactory();
        Transaction newTransaction = transactionFactory.newTransaction(session.getConnection(), false);
        ApiPublicMiddleTableMapper mMapper = session.getMapper(ApiPublicMiddleTableMapper.class);
        try {
            //搞完之后，插入数据
            //查询默认试题
            HashMap<String, Object> paraMap = new HashMap<String, Object>();
            paraMap.put(Const4PublicDataBase.PARA_type_middle_app, Const4EnumInDB.type_middle_app_class_info);
            paraMap.put(Const4PublicDataBase.PARA_type_inner_app, 10014);//查询试卷
            paraMap.put(Const4PublicDataBase.PARA_is_online, 0);//
            paraMap.put(Const4PublicDataBase.PARA_mid_table_remark_015+ "_in", mid_table_remark_015);
            HashMap<String, Object> realPara = SelectTool.paramap2selectMap(paraMap, "public_middle_table_lite_mod_exam_system");
            List<PublicMiddleTable> shijuanList = mMapper.tableDataList(realPara);//查询是否具备值

            if (shijuanList == null || shijuanList.size() == 0)
            {
                resultData.setMsg("试卷不存在");
                return resultData;
            }

            PublicMiddleTable shijuanOutline = shijuanList.get(0);
            //查询全部考题
            paraMap = new HashMap<String, Object>();
            paraMap.put(Const4PublicDataBase.PARA_type_middle_app, Const4EnumInDB.type_middle_app_class_info);
            paraMap.put(Const4PublicDataBase.PARA_type_inner_app, 10115);//题库
            paraMap.put(Const4PublicDataBase.PARA_mid_table_remark_015+ "_in", mid_table_remark_015);
            paraMap.put(Const4PublicDataBase.PARA_mid_table_remark_022, null);//全部统计
            paraMap.put(Const4PublicDataBase.PARA_is_online, 0);//
            realPara = SelectTool.paramap2selectMap(paraMap, "public_middle_table_lite_mod_exam_system");
            List<PublicMiddleTable> all_list = mMapper.tableDataList(realPara);//


            if (all_list == null || all_list.size() == 0)
            {
                resultData.setMsg("试卷没有考题");
                return resultData;
            }

            float all_sum = all_list.size();
            paraMap = new HashMap<String, Object>();
            paraMap.put(Const4PublicDataBase.PARA_type_middle_app, Const4EnumInDB.type_middle_app_class_info);
            paraMap.put(Const4PublicDataBase.PARA_type_inner_app, 10115);//题库
            paraMap.put(Const4PublicDataBase.PARA_mid_table_remark_015+ "_in", mid_table_remark_015);
            paraMap.put(Const4PublicDataBase.PARA_mid_table_remark_022, 101);//正确的考题
            paraMap.put(Const4PublicDataBase.PARA_is_online, 0);//

            realPara = SelectTool.paramap2selectMap(paraMap, "public_middle_table_lite_mod_exam_system");
            List<PublicMiddleTable> right_kaoti = mMapper.tableDataList(realPara);//


            float right_sum = 0;
            if (right_kaoti != null && right_kaoti.size() > 0)
            {
                right_sum = right_kaoti.size();
            }

            float final_result = right_sum / all_sum;

            final_result = (float)NumberTool.in5out4(final_result, 2); //四舍五入

            //更新试卷
            paraMap = new HashMap<String, Object>();
            paraMap.put(Const4PublicDataBase.PARA_mid_table_remark_004, final_result);
            paraMap.put(Const4PublicDataBase.PARA_time_4_end, new Date());
            paraMap.put("pre_db_para_id_public_middle_table", shijuanOutline.getId_public_middle_table());
            mMapper.updatePublicMiddleTable(UpdateTool.paramap2updateMap(paraMap, "public_middle_table_lite_mod_exam_system"));


            session.commit();

            //查询试卷
            paraMap = new HashMap<String, Object>();
            paraMap.put(Const4PublicDataBase.PARA_type_middle_app, Const4EnumInDB.type_middle_app_class_info);
            paraMap.put(Const4PublicDataBase.PARA_type_inner_app, 10014);//搜索题库
            paraMap.put(Const4PublicDataBase.PARA_mid_table_remark_015+ "_in", mid_table_remark_015);
            paraMap.put(Const4PublicDataBase.TABLE_NAME_4_MIDDLE, Const4PublicDataBase.TABLE_NAME_4_MIDDLE_DEFAULT);

            realPara = SelectTool.paramap2selectMap(paraMap, "public_middle_table_lite_mod_exam_system");
            List<PublicMiddleTable> shijuan = mMapper.tableDataList(realPara);//
            //查询考题
            paraMap = new HashMap<String, Object>();
            paraMap.put(Const4PublicDataBase.PARA_type_middle_app, Const4EnumInDB.type_middle_app_class_info);
            paraMap.put(Const4PublicDataBase.PARA_type_inner_app, 10115);//搜索题库
            paraMap.put(Const4PublicDataBase.PARA_mid_table_remark_015+ "_in", mid_table_remark_015);
            paraMap.put(Const4PublicDataBase.TABLE_NAME_4_MIDDLE, Const4PublicDataBase.TABLE_NAME_4_MIDDLE_DEFAULT);
            realPara = SelectTool.paramap2selectMap(paraMap, "public_middle_table_lite_mod_exam_system");
            List<PublicMiddleTable> kaoti = mMapper.tableDataList(realPara);//

            HashMap<String, Object> returnData = new HashMap<String, Object>();
            returnData.put("shijuan", shijuan);
            returnData.put("shijuan_kaoti", kaoti);

            resultData.setStatus(0);
            resultData.setReturnData(returnData);
            return resultData;
        }
        catch(Exception e)
        {
            try {
                newTransaction.rollback();
                String msg = Tool_4_ServletApi.showStackTrace(e);
                resultData.setMsg(msg);
                return resultData;

            } catch (SQLException e1) {
                String msg = Tool_4_ServletApi.showStackTrace(e1);
                resultData.setMsg(msg);
                return resultData;
            }
        }
        finally
        {
            session.close();
        }
    }







}